import { observable, action, runInAction, configure, autorun } from 'mobx'
import { getTagList, addArticle, detailArticle, updateArticle} from './ArticleCreateServ' 
import moment from 'moment'
import {message} from 'antd'

class ArticleCreate {
	@observable steps = [
		{
			title: '文章细节',
			content: 'First-content'
		},
		{
			title: "文章内容",
			content: 'Second-content'
		},
		{
			title: "文章预览",
			content: 'Third-content'
		}
	]

	@observable categoryList = []


	@observable current = 0
	@observable type = ''

	@observable defaultform = {
		articleTitle: '',
		articleCreateTime: '',
		articleCategory: [],
		articleDesc: ''
	}

	@observable form = {
		articleTitle: '',
		articleCreateTime: '',
		articleCategory: [],
		articleDesc: '',
		id: ''
	}

	@observable fuhtml = ''

	@action
	doGetTagList = () => {
		getTagList().then((res) => {
			runInAction(() => {
				let tempList = []
				res.data.list.forEach((item, index) => {
					tempList.push(item.categoryName)
				})
				console.log(this, '--====')
				this.categoryList.replace(tempList)
			})
			console.log('获取数据完成')
		}, (err) => {
			console.log(err)
		})
	}

	@action 
	doAddArticle = () => {

		return addArticle({
			articleTitle: this.form.articleTitle,
			articleTime: this.form.articleCreateTime,
			articleTag: this.form.articleCategory,
			articleDesc: this.form.articleDesc,
			articleContent: this.fuhtml
		}).then((res) => {
			if(res.resultCode + '' === '0') {
				message.success('添加成功')
				return Promise.resolve(res)
			}
		}, (err) => {
		})
	}

	@action
	doDetailArticle = (params) => {
		detailArticle(params).then((res) => {
			runInAction(() => {
				this.form.articleTitle = res.data.articleTitle
				this.form.articleCreateTime= res.data.articleTime
				this.form.articleCategory = res.data.articleTag
				this.form.articleDesc = res.data.articleDesc
				this.form.id = res.data._id
				this.fuhtml = res.data.articleContent
			})
		}, (err) => {
			console.log(err)
		})
	}

	@action
	doUpdateArticle = (params) => {
		return updateArticle({
			id: this.form.id,
			articleTitle: this.form.articleTitle,
			articleTime: this.form.articleCreateTime,
			articleTag: this.form.articleCategory,
			articleDesc: this.form.articleDesc,
			articleContent: this.fuhtml
		}).then((res) => {
			return Promise.resolve(res)
		}, (err) => {
			console.log(err)
		})
	}


}


export default ArticleCreate